package com.tpp.controller;


import com.tpp.entity.*;
import com.tpp.service.FilmService;
import com.tpp.service.UserService;
import com.tpp.util.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CFilm {
    @Autowired
    private FilmService filmService;
    @RequestMapping("show_film")
    public String showFilm(HttpServletRequest request, Model model){
        String Film_name = request.getParameter("Film_name");
        System.out.println("Film_name"+Film_name);
        Film film=filmService.GetFilmInfo(Film_name);
        System.out.println(film);

        model.addAttribute("film_info", film);

        return "/static/film";
    }
    @RequestMapping("FindAFilmTimeByTitle")
    @ResponseBody
    public Result FindAFilmTimeByTitle(HttpServletRequest request){
        String filmTitle = request.getParameter("filmTitle");
        List<FilmTime> times=filmService.FindAFilmTimeByTitle(filmTitle);
        Result result=new Result();
        if(times.size()==0) {
            result.setData(times);
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(times);
            result.setMsg("查询成功");
            result.setStatus(0);
        }

       /* JSONObject json = JSONObject.fromObject(result);
        System.out.println(json.toString());*/
        System.out.println(result.toString());
        return  result;
    }
    @RequestMapping("FindAFilmTimeByCinema")
    @ResponseBody
    public Result FindAFilmTimeByAddress(HttpServletRequest request){
        String cinema = request.getParameter("cinema");
        String filmTitle = request.getParameter("filmTitle");
        Date[] times=filmService.FindAFilmTimeByCinema(cinema,filmTitle);
        System.out.println("cinema:"+cinema+"filmTitle:"+filmTitle);
        for(Date x:times){
            System.out.print(x+"|");
        }
        System.out.println(times.length);
        Result result=new Result();
        result.setData(times);
        System.out.println(result.toString());
        return result;
    }
    @RequestMapping("FindAFilmTimeByDate")
    @ResponseBody
    public Result FindAFilmTimeByDate(HttpServletRequest request){
        Date date = strToDate(request.getParameter("date"));
        String filmTitle = request.getParameter("filmTitle");
        List<FilmTime> times=filmService.FindAFilmTimeByDate(date,filmTitle);

       /* for(FilmTime x:times){
            System.out.print(x+"|");
        }*/
        Result result=new Result();

        if(times.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(times);
            result.setMsg("查询成功");
            result.setStatus(0);
        }

       /* JSONObject json = JSONObject.fromObject(result);
        System.out.println(json.toString());*/
        System.out.println(result.toString());
        return  result;
    }
    @RequestMapping("FindonLineFilms")
    @ResponseBody
    public Result FindonLineFilms(HttpServletRequest request){

        List<FilmItemShow> Films=filmService.findOnlineFilms();
        Result result=new Result();

        if(Films.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(Films);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    @RequestMapping("FindForthcomingFilms")
    @ResponseBody
    public Result FindForthcomingFilms(HttpServletRequest request){
        List<FilmItemShow> Films=filmService.FindForthcomingFilms();
        Result result=new Result();

        if(Films.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(Films);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    @RequestMapping("findAllForthcomingFilms")
    @ResponseBody
    public Result findAllForthcomingFilms(HttpServletRequest request){

        List<FilmItemShow> Films=filmService.findAllForthcomingFilms();
        Result result=new Result();

        if(Films.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(Films);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    @RequestMapping("findAllOnlineFilms")
    @ResponseBody
    public Result findAllOnlineFilms(HttpServletRequest request){

        List<FilmItemShow> Films=filmService.findAllOnlineFilms();
        Result result=new Result();

        if(Films.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(Films);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    @RequestMapping("findSeatInfoByTimesId")
    @ResponseBody
    public Result findSeatInfoByTimesId(HttpServletRequest request){
        int time_id = Integer.valueOf(request.getParameter("time_id"));
        String seatInfo=filmService.findSeatInfoByTimesId(time_id);
        Result result=new Result();

        if(seatInfo=="") {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(seatInfo);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    public static java.sql.Date strToDate(String strDate) {
        String str = strDate;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date d = null;
        try {
            d = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.sql.Date date = new java.sql.Date(d.getTime());
        return date;
    }
    @RequestMapping("findAllOnlineFilms_wx")
    @ResponseBody
    public Result findAllOnlineFilms_wx(HttpServletRequest request){

        List<Film> Films=filmService.findAllOnlineFilms_wx();
        Result result=new Result();

        if(Films.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(Films);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    @RequestMapping("findAllforthcomingFilms_wx")
    @ResponseBody
    public Result findAllforthcomingFilms_wx(HttpServletRequest request){

        List<Film> Films=filmService.findAllforthcomingFilms_wx();
        Result result=new Result();

        if(Films.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(Films);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    @RequestMapping("getFilmInfoById_wx")
    @ResponseBody
    public Result getFilmInfoById_wx(HttpServletRequest request){
        int film_id = Integer.valueOf(request.getParameter("film_id"));
        Film Film=filmService.getFilmInfoById_wx(film_id);
        Result result=new Result();

        if(Film==null) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(Film);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }

    @RequestMapping("getOrderInfoByUsername_wx")
    @ResponseBody
    public Result getOrderInfoByUsername_wx(HttpServletRequest request){
        String username = request.getParameter("username");
        List<Order> order=filmService.getOrderInfoByUsername_wx(username);
        Result result=new Result();

        if(order.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(order);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }

    @RequestMapping("getAllTimesByFilmName_wx")
    @ResponseBody
    public Result getAllTimesByFilmName_wx(HttpServletRequest request){
        String filmName = request.getParameter("filmName");
        List<FilmTime> order=filmService.getAllTimesByFilmName_wx(filmName);
        Result result=new Result();

        if(order.size()==0) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(order);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }
    @RequestMapping("getSeatInfoByTimeId_wx")
    @ResponseBody
    public Result getSeatInfoByTimeId_wx(HttpServletRequest request){
        int timeId = Integer.valueOf(request.getParameter("timeId"));
        String seatIfo=filmService.getSeatInfoByTimeId_wx(timeId);
        System.out.println(seatIfo);

        int count=0;
        String[] seatArr=seatIfo.split("");
        List<SeatItem> seats=new ArrayList<>();
        for(int i=0;i<seatArr.length;i++){
            if(seatArr[i].equals("0")||seatArr[i].equals("1")){

                if(seatArr[i].equals("0")){
                    //System.out.println("test");
                    if(count>36){
                        int x=3+(count-36)/10+1;
                        int y=(count-36)%10+1;
                        SeatItem Seatnew=new SeatItem(x+"排"+y+"列",x,y);
                        seats.add(Seatnew);
                        System.out.println(Seatnew+">");
                    }else {
                        int x=count/12+1;
                        int y=count%12+1;
                        SeatItem Seatnew=new SeatItem(x+"排"+y+"列",x,y);
                        seats.add(Seatnew);
                        System.out.println(Seatnew+"<");
                    }
                }
                count++;
            }
        }

        Result result=new Result();
        if(seatIfo.equals("")) {
            result.setMsg("查询错误");
            result.setStatus(1);
        }else {
            result.setData(seats);
            result.setMsg("查询成功");
            result.setStatus(0);
        }
        return result;
    }

}
